<template>
  <view class="content">
        <view class="content-header"><image src="https://small.qaqavr.com/img/zhongrui/bg.png" mode=""></image></view>
    <view class="content-form">
      <u--form labelPosition="left" :borderBottom="false" :model="form" labelWidth="100px" :rules="rules" ref="formRef">
        <u-form-item>推荐人</u-form-item>
        <u-form-item label="姓名" borderBottom ref="item1"><u--input v-model="form.jobSeekerByRecommendedDto.realName" border="none"></u--input></u-form-item>
        <u-form-item label="电话" borderBottom ref="item1"><u--input v-model="form.jobSeekerByRecommendedDto.telPhone" type="number" border="none"></u--input></u-form-item>
        <u-form-item v-if="id == 1" label="工号" borderBottom ref="item1"><u--input v-model="form.jobSeekerByRecommendedDto.workNum"  disabled disabledColor="#ffffff" border="none"></u--input></u-form-item>
        <u-form-item v-else label="身份证" borderBottom ref="item1"><u--input v-model="form.jobSeekerByRecommendedDto.idCard" type="idcard" border="none"></u--input></u-form-item>
        <u-form-item>被推荐人</u-form-item>
        <u-form-item label="姓名" borderBottom ref="item1"><u--input v-model="form.realName" border="none"></u--input></u-form-item>
        <u-form-item label="电话" borderBottom ref="item1"><u--input v-model="form.telPhone" border="none" type="number"></u--input></u-form-item>
        <u-form-item label="身份证号" borderBottom ref="item1"><u--input v-model="form.idCard" type="idcard" border="none"></u--input></u-form-item>

        <u-form-item label="面试职位" borderBottom ref="item1"><u--input disabled disabledColor="#fff" v-model="form.jobName" border="none"></u--input></u-form-item>

        <u-form-item label="岗位编号" borderBottom ref="item1"><u--input disabled disabledColor="#fff" v-model="form.jobNum" border="none"></u--input></u-form-item>

        <u-form-item label="面试日期" borderBottom @click="showDate = true" ref="item1">
          <u--input v-model="form.interviewTime" disabled disabledColor="#ffffff" border="none"></u--input>
          <u-icon slot="right" name="arrow-right"></u-icon>
        </u-form-item>
      </u--form>

      <u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>

      <u-datetime-picker :show="showDate" :value="dateTime" mode="date" closeOnClickOverly @confirm="dateTimechange" @cancel="dateClose" @close="dateClose"></u-datetime-picker>
    </view>

    <!-- 是否保存到我的信息的弹框 -->
    <view v-if="show" class="mymessage">
      <view class="showheader">提示</view>
      <view class="showtext">是否保存当前登记信息至个人中心</view>
      <view class="showbtns">
        <view class="btns" style="background-color: #02a7f0;" @click="getchange">确定</view>
        <view class="btns" style="background-color: #6fb83e;" @click="dongetchange">取消</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {   
  data() {
    return {
      form: {
        graduationTime: '', //毕业时间
        idCard: '', //身份证
        interviewTime: '', //面试时间
        isUpdate: null, //是否更新用户信息  1是 0否
        jobNum: '', //面试职位代号
        jobTypeId: null, //面试职位类型ID
        jobName: '',
        realName: '', //名字
        recruitmentType: 1, // 1社招 2校招
        schoolProveUrl: '', //学历证明图片地址
        signUpType: null, // 1内部推荐 2外部推荐
        telPhone: '', //电话
        // 用户信息
        jobSeekerByRecommendedDto: {
          avatarUrl: '',
          graduationTime: '',
          idCard: '',
          openId: '',
          realName: '',
          schoolProveUrl: '',
          telPhone: '',
          workNum: ''
        }
      },
      workdetails: {},
      showDate: false, //控制面试时间
      show: false,
      userList: {}, //用户的基本信息
      dateTime: Number(new Date()),
      id: null,
      // 验证规则
      rules: {
        'form.realName': {
          type: 'string',
          required: true,
          message: '请填写姓名',
          trigger: ['blur', 'change']
        },
        'jobSeekerBySelfDto.telPhone': {
          type: 'string',
          required: true,
          message: '请填写电话',
          trigger: ['blur', 'change']
        },
        'jobSeekerBySelfDtos.idCard': {
          type: 'string',
          required: true,
          message: '请填写身份证',
          trigger: ['blur', 'change']
        },
        'userInfo.sex': {
          type: 'string',
          required: true,
          message: '请填写岗位类别',
          trigger: ['blur', 'change']
        },
        'userInfo.code': {
          type: 'string',
          required: true,
          message: '请填写岗位编号',
          trigger: ['blur', 'change']
        },
        'userInfo.birthday': {
          type: 'string',
          required: true,
          message: '请选择日期',
          trigger: ['change']
        }
      }
    };
  },
  onLoad(options) {
    this.form.signUpType = options.id;
    this.id = options.id;
    this.userList = uni.getStorageSync('userinfo');
    console.log('options.id', options.id);
    this.workdetails = JSON.parse(options.workDetails);
    console.log('JSON.parse(options.workDetails)', JSON.parse(options.workDetails));
    console.log('我是用户基本信息===>', this.userList);
    this.updateuserlist();

    let pages = getCurrentPages();
    // 获取上一页栈
    let prevPage = pages[pages.length - 2];
    // 触发上一页 upData 函数(并携带参数)
    prevPage.$vm.closetankuang();
  },  
   onShow() {
    this.updateuserlist();
  },

  methods: {
    // 给表单赋值用户基本信息
    updateuserlist() {
      this.form.jobSeekerByRecommendedDto.realName = this.userList.realName;
      this.form.jobSeekerByRecommendedDto.graduationTime = this.userList.graduationTime;
      this.form.jobSeekerByRecommendedDto.avatarUrl = this.userList.avatarUrl;
      this.form.jobSeekerByRecommendedDto.telPhone = this.userList.telPhone;
      this.form.jobSeekerByRecommendedDto.idCard = this.userList.idCard;
      this.form.jobSeekerByRecommendedDto.schoolProveUrl = this.userList.schoolProveUrl;
      this.form.jobSeekerByRecommendedDto.workNum = this.userList.workNum;
      this.form.jobSeekerByRecommendedDto.openId = this.userList.openId;
      this.form.jobTypeId = this.workdetails.jobType.id;
      this.form.jobName = this.workdetails.jobType.typeName;
      this.form.jobNum = this.workdetails.jobNum;
    },
    dateClose() {
      this.showDate = false;
      this.$refs.formRef.validateField('userInfo.birthday');
    },
    dateTimechange(e) {
      this.showDate = false;
      this.form.interviewTime = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
      this.$refs.formRef.validateField('userInfo.birthday');
    },
    submit() {
      if(this.id==1){
         var userinfo = uni.getStorageSync('userinfo');
         if(!userinfo.idCard){
          uni.showToast({
            title: '请完善个人中心身份证信息',
            icon: 'none'
          });
          return;
         }
         if(!userinfo.workNum){
             uni.showToast({
               title: '您当前不允许内部推荐',
               icon: 'none'
             });
             return;
            }
        }
      // 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
      this.$refs.formRef
        .validate()
        .then(res => {
          //uni.$u.toast('校验通过')
          console.log('this.userList', this.userList);
          console.log('this.form', this.form);
          const Obj = {
            avatarUrl: this.userList.avatarUrl,
            graduationTime: this.userList.graduationTime,
            idCard: this.userList.idCard,
            openId: this.userList.openId,
            realName: this.userList.realName,
            schoolProveUrl: this.userList.schoolProveUrl,
            telPhone: this.userList.telPhone,
            workNum: this.userList.workNum
          };
          console.log('objobj', Obj);
          if (JSON.stringify(this.form.jobSeekerByRecommendedDto) === JSON.stringify(Obj)) {
            this.form.isUpdate = 0;
            // this.form.jobTypeId = this.workdetails.jobType.id
            // const { data } =  uni.$http.post('/sign/signUpAddByRecommended' , this.form)
            uni.request({
              url: uni.$http.baseUrl + '/sign/signUpAddByRecommended',
              method: 'POST',
              data: this.form,
              header: {
                token: uni.getStorageSync('tokenValue')
              },
              success(res) {
                console.log('提交提交', res.data);
                if (res.data.statusCode !== 200) {
                 uni.showToast({
                   title: res.data.message,
                   icon: 'none'
                 });
                } else {
                  uni.showToast({
                    title: res.data.message,
                    icon: 'success'
                  });
                  setTimeout(function() {
                    uni.navigateBack();
                  }, 700);
                }
              }
            });
          } else {
            this.show = true;
          }
        })
        .catch(errors => {
          uni.$u.toast('校验失败');
        });
    },
    open() {
      console.log('open');
    },
    close() {
      this.show = false;
      console.log('close');
    },
    // 更新用户信息并提交
    async getchange() {
      this.show = false;
      this.form.isUpdate = 1;
      // this.form.jobTypeId = this.workdetails.jobType.id
      const res = await uni.$http.post('/sign/signUpAddByRecommended', this.form);
      console.log('提交提交', this.form);
      console.log('res9999', res);
      console.log('提交提交', res.data);
      if (res.data.statusCode !== 200) {
        uni.showToast({
          title: res.data.message,
          icon: 'none'
        });
      } else {
        uni.setStorageSync('userinfo', this.form.jobSeekerByRecommendedDto);
        uni.showToast({
          title: res.data.message,
          icon: 'success'
        });
        setTimeout(function() {
          uni.navigateBack();
        }, 700);
      }
    },
    // 不更新用户信息并提交
    async dongetchange() {
      this.show = false;
      this.form.isUpdate = 0;
      // this.form.jobTypeId = this.workdetails.jobType.id
      const res = await uni.$http.post('/sign/signUpAddByRecommended', this.form);
      console.log('提交提交', this.form);
      console.log('res', res);
      console.log('提交提交', res.data);
      if (res.data.statusCode !== 200) {
        uni.showToast({
          title: res.data.message,
          icon: 'none'
        });
      } else {
        uni.showToast({
          title: res.data.message,
          icon: 'success'
        });
        setTimeout(function() {
          uni.navigateBack();
        }, 700);
      }
    }
  }
};
</script>

<style lang="scss">
.content {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #ccc;

    .content-header {
      // height: 500rpx;
      image {
        width: 100%;
        height: 350rpx;
      }
    }

  .content-form {
    position: fixed;
    top: 5%;
    left: 10%;
    padding: 20rpx 40rpx;
    box-sizing: border-box;
    width: 80%;
    border-radius: 30rpx;
    background-color: #ffffff;

    input {
      height: 60rpx;
      font-size: 20rpx;
    }

    .u-form-item__body__left__content__label.data-v-5e7216f1 {
      font-size: 24rpx;
    }

    .u-form-item__body.data-v-5e7216f1 {
      padding: 5px;
    }
  }

  .u-popup-slot {
    width: 200px;
    height: 150px;
    padding: 20rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    view {
      margin: 20rpx 0;
    }
  }
  .mymessage {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 500rpx;
    height: 300rpx;
    border-radius: 30rpx;
    background-color: #ffffff;
    overflow: hidden;
    box-shadow: 0 0 5rpx #ccc;
    .showheader {
      height: 80rpx;
      background-color: #e8e8e8;
      font-size: 38rpx;
      text-align: center;
      line-height: 80rpx;
    }
    .showtext {
      margin-top: 20rpx;
      height: 80rpx;
      background-color: #ffffff;
      text-align: center;
      font-size: 24rpx;
      line-height: 80rpx;
    }
    .showbtns {
      margin-top: 20rpx;
      height: 80rpx;
      display: flex;
      padding: 0 20rpx;
      background-color: #ffffff;
      justify-content: space-around;
      .btns {
        width: 150rpx;
        height: 50rpx;
        border-radius: 5rpx;
        text-align: center;
        line-height: 50rpx;
        color: #ffffff;
      }
    }
  }
}
</style>
